import { S, U, getRequestId, mapField } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import env from '../../config/common'
import N from '../../lib/notification'
import { customFields, customSubmit } from '../mappings/customs'
import {
  UPDATE_DATA_DICTCTIONARY, UPDATE_SWITCH_SITE_STATUS, UPDATE_SITE_ID,
  UPDATE_UPDATE_SMALL_AGE_CUSTOM_QUERY_CONDITION,
  UPDATE_UPDATE_LARGE_AGE_CUSTOM_QUERY_CONDITION,
  UPDATE_DAO_FONG_QUERY_CONDITION,
  UPDATE_TEAM_HIGH_SEAS_QUERY_CONDITION
} from '../mutation-types'

export default {
  // 获取客户列表
  getCustoms ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/customs' + frag
      ).then((response) => {
        if (response.data.results) {
          const customs = mapField(response.data.results.data, customFields, info)
          response.data.results.data = customs
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getIncomeCustoms ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/income_customs' + frag
      ).then((response) => {
        // if (response.data.results) {
        //   const customs = mapField(, customFields, info)
        //   response.data.results.data = response.data.results.data
        // }
        success(response.data)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getCustomsStatus ({ dispatch, commit, state, rootState }, info) {
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_HOME_API_HOST + '/data_dictionary/custom_status'
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  gonghai ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    console.log(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/gonghai' + frag
      ).then((response) => {
        if (response.data.results) {
          const customs = mapField(response.data.results.data, customFields, info)
          response.data.results.data = customs
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  getCustom ({ dispatch, commit, state, rootState }, info) {
    const params = '?' + U()
    let frag = ''
    if (typeof info['id'] !== 'undefined') {
      frag = '/' + info['id']
    }
    console.log(frag)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/customs' + frag + params
      ).then((response) => {
        if (response.data.results) {
          const customs = mapField(response.data.results, customFields, info)
          response.data.results = customs
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  saveCustom ({ dispatch, commit, state, rootState }, info) {
    let frag = '?' + U()
    if (typeof info['id'] !== 'undefined') {
      frag = '/' + info['id']
    }
    console.log(JSON.stringify(info))
    info = mapField(info, customSubmit)
    const params = S(info)
    return Q.Promise((success, error) => {
      axios.post(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/customs' + frag,
        params
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  getExports ({ dispatch, commit, state, rootState }, info) {
    return Q.Promise((success, error) => {
      axios.get(
        env.EXPORT_API_HOST + '/sites/' + rootState.auth.site_id + '/users/' + rootState.auth.user.suid + '/customs/export'
      ).then((response) => {
        if (response.data.results) {
          const data = response.data.results
          response.data.results = data
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  // 导出客户名单任务接口
  saveExport ({ dispatch, commit, state, rootState }, info) {
    console.log(JSON.stringify(info))
    const params = S({
      info: info,
      trace: {
        requestId: getRequestId()
      }
    })
    return Q.Promise((success, error) => {
      axios.post(
        env.EXPORT_API_HOST + '/sites/' + rootState.auth.site_id + '/users/' + rootState.auth.user.suid + '/customs/export',
        params,
        { headers: {
          'Content-Type': 'application/json;charset=UTF-8'
        }}
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  // 获取数据字典
  getDictionary ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      if (Object.keys(rootState.custom['data_dictionary']).length > 0) {
        success(rootState.custom['data_dictionary'])
      }
      axios.get(
        env.CRM_API_HOST + '/data_dictionary' + frag
      ).then((response) => {
        if (response.data.results) {
          const data = response.data.results
          response.data.results = data
          commit(UPDATE_DATA_DICTCTIONARY, data)
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  // 获取数据字典
  getSites ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/index' + frag
      ).then((response) => {
        if (response.data.results) {
          const data = response.data.results
          response.data.results = data
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  switchSite ({ dispatch, commit, state, rootState }, info) {
    let frag = '?' + U()
    const params = S(info)
    if (typeof info['site_id'] !== 'undefined') {
      frag = '/' + info['site_id']
    }
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/switch_site' + frag,
        params
      ).then((response) => {
        commit(UPDATE_SITE_ID, info['site_id'])
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  showSwitchSite ({ commit }, info) {
    commit(UPDATE_SWITCH_SITE_STATUS, true)
  },

  hideSwitchSite ({ commit }, info) {
    commit(UPDATE_SWITCH_SITE_STATUS, false)
  },
  getHighSeasTypeCustomList ({ dispatch, commit, state, rootState }, info) {
    let URL
    if (info.high_seas_type === 'XL') {
      URL = env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/sites/' + rootState.auth.user.site_id + '/small/age/custom'
    } else {
      URL = env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/sites/' + rootState.auth.user.site_id + '/large/age/custom'
    }
    return axios.get(URL + '?' + U(info))
  },
  updateSmallAgeCustomQueryCondition ({ commit }, data) {
    commit(UPDATE_UPDATE_SMALL_AGE_CUSTOM_QUERY_CONDITION, data)
  },
  updateLargeAgeCustomQueryCondition ({ commit }, data) {
    commit(UPDATE_UPDATE_LARGE_AGE_CUSTOM_QUERY_CONDITION, data)
  },
  updateDaoFongQueryCondition ({ commit }, data) {
    commit(UPDATE_DAO_FONG_QUERY_CONDITION, data)
  },
  getDaoFangList ({ dispatch, commit, state, rootState }, info) {
    const URL = env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/sites/' + rootState.auth.user.site_id + '/df/clue/list'
    return axios.get(URL + '?' + U(info))
  },
  getTeamHighSeasCustoms ({ dispatch, commit, state, rootState }, info) {
    const URL = env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/sites/' + rootState.auth.user.site_id + '/team/high_seas/customs'
    return axios.get(URL + '?' + U(info))
  },
  updateTeamHighSeasQueryCondition ({ commit }, data) {
    commit(UPDATE_TEAM_HIGH_SEAS_QUERY_CONDITION, data)
  },
  uploadCustomsExportExcel ({ dispatch, commit, state, rootState }, info) {
    info.site_id = rootState.auth.user.site_id
    const params = S({
      body: {
        exportId: 'customreIncomeList',
        param: info,
        exportInfo: {
          site_id: rootState.auth.user.site_id,
          optsuid: rootState.auth.user.suid
        }
      },
      trace: {
        requestId: getRequestId()
      },
      app: {
        appId: 'xxx'
      }
    })
    return Q.Promise((success, error) => {
      axios.post(
        env.EXPORT_API_HOST + '/excel/exportFromTemplate',
        params,
        { headers: { 'Content-Type': 'application/json;charset=UTF-8' }}
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  }
}
